package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.ProductBean;
import com.project.mapper.IProductMapper;
import com.project.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private IProductMapper mapper;

    @Override
    public void add(ProductBean product) {
        mapper.insert(product);
    }

    @Override
    public void del(Integer id) {
        mapper.deleteById(id);
    }

    @Override
    public void update(Integer id, Integer price) {
        ProductBean productBean = new ProductBean();
        productBean.setId(id);
        productBean.setPrice(price);
        mapper.updateById(productBean);
    }

    @Override
    public List<ProductBean> findAll() {
        return mapper.selectList(null);
    }

    @Override
    public ProductBean findById(Integer id) {
        return mapper.selectById(id);
    }

    @Override
    public IPage<ProductBean> findByItem(Integer pageNO, String name, LocalDate startDate, LocalDate endDate) {
        QueryWrapper<ProductBean> qw = new QueryWrapper<>();
        if (name != null & name.length() != 0) {
            qw.like("p_name", name);
        }
        if (startDate != null) {
            qw.ge("p_createDate", startDate);
        }
        if (endDate != null) {
            qw.le("p_createDate", endDate);
        }


        return mapper.selectPage(new Page<>(pageNO, 3), qw);
    }
}
